package comm_ctrl

import (
	"fmt"
	"io"
	"os"
	"time"
)

func SimpleUpload() {
	if len(os.Args) < 2 {
		fmt.Println("usage: reader <file>")
		return
	}
	filename := os.Args[1]

	start := time.Now()
	if err := readBigFile(filename); err != nil {
		fmt.Println("read error:", err)
		return
	}
	elapsed := time.Since(start)
	fmt.Println("cost:", elapsed)
}

func readBigFile(path string) error {
	f, err := os.Open(path)
	if err != nil {
		return err
	}
	defer f.Close()

	// 4MB buffer，按实际情况可以调成 8MB / 16MB
	buf := make([]byte, 4<<20)

	var total int64
	for {
		n, err := f.Read(buf)
		if n > 0 {
			// 这里可以做一些简单处理，比如校验和，而不是转成 string
			total += int64(n)
		}
		if err == io.EOF {
			break
		}
		if err != nil {
			return err
		}
	}

	fmt.Printf("total read: %.2f GB\n", float64(total)/(1024*1024*1024))
	return nil
}
